<?php
defined('SYS_RUN') or exit();

class Gzip {

    public function compressOutput() {
        $content = ob_get_contents();
        ob_clean();
        echo $this->_compress($content);
    }

    public function compressContent($content) {
        return $this->_compress($content);
    }

    private function _compress($content) {
        if (function_exists('gzencode')) {
            $gzipContent = gzencode($content, 7, FORCE_GZIP);
        } else {
            if (function_exists('gzcompress') && function_exists('crc32')) {
                $size = strlen($content);
                $crc = crc32($content);
                $gzipContent = "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff";
                $gzipContent .= substr(gzcompress($content,7), 2, -4);
                $gzipContent .= pack('V', $crc);
                $gzipContent .= pack('V', $size);
            }
        }

        if (isset($gzipContent)) {
            header("Content-Encoding: gzip");
        }
        return (isset($gzipContent) ? $gzipContent : $content);
    }
}